<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 资源位ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   allen.qiang
 */

quanli_require('module/base/content/source.class.php');

class ajax_source {

    public function execute() { 

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //编辑前端资源
                    $this->editSource();
                    break;
                case 2: //刷新缓存
                    $this->refreshCache();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }

        } else {

            returnJsonData('参数错误');

        }

    }

    private function editSource() {

        global $G_PGLOB;

        //实例资源类
        $source = new source();

        //保存资源位
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $result_array = $source->editSource( $_REQUEST );

        echo json_encode($result_array);

    }

    //删除静态缓存文件 add by allen.qiang 2015-05-26 Start
    private function refreshCache() {

        //资源类型
        $page_type = $_REQUEST['page_type'];//1:PC端 2:wap端

        $city_id = $_REQUEST['cityID'];

        if($page_type == 1){
            //缓存文件地址
            $cachehtmlfile = G_APPLICATION_BASEPATH . "shop/html_cache/index/$city_id/index.html";
        }else{
            //缓存文件地址
            $cachehtmlfile = G_APPLICATION_BASEPATH . "wap/html_cache/index/$city_id/index.html";
        }

        //判断是不是文件 判断文件是否存在
        if(is_file($cachehtmlfile) && file_exists($cachehtmlfile)){

            $result = @unlink($cachehtmlfile);
            if($result){
                echo json_encode(array('code'=>200,'message'=>'刷新成功'));
            }else{
                echo json_encode(array('code'=>400,'message'=>'刷新失败'));
            }

        }else{
            echo json_encode(array('code'=>400,'message'=>'尚未生成缓存'));
        }

    }

}